﻿using System;
using System.Windows.Forms;
using WindowsLive.Writer.Api;
using ColorCode;

namespace AnglicanGeek.LiveWriter
{
    [WriterPlugin("943002EB-7043-4c5f-A958-F505548BC5D5", "Code Colorizer", 
        Description = "Insert and colorize source code.", 
        ImagePath = "CodeColorizerPlugin.png")]
    [InsertableContentSource("Source Code", 
        SidebarText="Source Code")]
    public class CodeColorizerPlugin : ContentSource
    {
        IFormFactory formFactory;
        
        public CodeColorizerPlugin()
            : this(new FormFactory())
        {
        }

        public CodeColorizerPlugin(IFormFactory formFactory)
        {
            this.formFactory = formFactory;
        }
        
        public override DialogResult CreateContent(
            IWin32Window dialogOwner, 
            ref string content)
        {
            var dialog = formFactory.CreateInsertSourceCodeDialog();
            dialog.StartPosition = FormStartPosition.CenterParent;
            dialog.SetLanguageChoices(Languages.All);
            var dialogResult = dialog.Show(dialogOwner);

            if (dialogResult == DialogResult.OK)
                content = new CodeColorizer().Colorize(dialog.SourceCode, dialog.Language);

            return dialogResult;
        }
    }
}
